// Copyright 2011 Google Inc. All Rights Reserved.

package udrpc

import (
	"encoding/binary"
	"io"
	"os"
)

// Handles holds the list of opened files.
type Handles struct {
	fs []*os.File
}

// Add adds a file to the list.
func (h *Handles) Add(f *os.File) {
	h.fs = append(h.fs, f)
}

// Close closes all the files in the list.
func (h *Handles) Close() {
	for _, f := range h.fs {
		f.Close()
	}
}

// Len returns the number of files in the list.
func (h *Handles) Len() int {
	return len(h.fs)
}

func (h *Handles) saveTo(w io.Writer) (err os.Error) {
	for _, f := range h.fs {
		err = binary.Write(w, binary.LittleEndian, uint32(f.Fd()))
		if err != nil {
			return
		}
	}
	return
}
